<?php

namespace App\Repositories\Ca;

use App\Models\BaseModel;
use App\Models\Ca\HandoverRecord;
use App\Repositories\BaseRepository;
use App\Utils\Output;
use App\Utils\QueryHelper;

class HandoverRecordRepository extends BaseRepository
{

    /**
     * 分页查询
     * @param array $params
     * @return Output
     */
    function adminPager(array $params)
    {
        extract($params);
        $query = HandoverRecord::query();
        $query->leftJoin('system__users as from_user', 'from_user.id', '=', 'ca__handover_records.from_user_id');
        $query->leftJoin('system__users as to_user', 'to_user.id', '=', 'ca__handover_records.to_user_id');
        $query->leftJoin('system__users as user', 'user.id', '=', 'ca__handover_records.user_id');
        if (!empty($type)) {
            $query->where('ca__handover_records.type', '=', $type);
        }
        if (!empty($from)) {
            QueryHelper::like($query, 'from_user.name|from_user.mobile|from_user.username', $from);
        }
        if (!empty($to)) {
            QueryHelper::like($query, 'to_user.name|to_user.mobile|to_user.username', $to);
        }

        if (!empty($date_range)) {
            list($start, $end) = explode(' ~ ', $date_range);
            $query->whereBetween('ca__handover_records.created_at', [$start, $end]);
        }

        $totalQuery = clone $query;
        $total = $totalQuery->count();
        $query->select(
            'ca__handover_records.id',
            'ca__handover_records.created_at',
            'ca__handover_records.data_count',
            'ca__handover_records.type as type_text',
            'from_user.name as from_name',
            'to_user.name as to_name',
            'user.name',
        );
        $lis = $this->setParams($query, $params, ['id' => 'desc']);
        return Output::pager($lis, $total);
    }

    function caller($from, $to)
    {

    }

    function getModel(): BaseModel
    {
        return new HandoverRecord();
    }
}